package com.zhuyi.broadcast.controller;

import com.zhuyi.broadcast.pojo.domain.DeviceCategory;
import com.zhuyi.broadcast.service.DeviceCategoryService;
import com.zhuyi.common.annotation.Log;
import com.zhuyi.common.core.controller.BaseController;
import com.zhuyi.common.core.domain.AjaxResult;
import com.zhuyi.common.core.page.TableDataInfo;
import com.zhuyi.common.enums.BusinessType;
import com.zhuyi.common.utils.SecurityUtils;
import com.zhuyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  设备类型Controller
 * </p>
 *
 * @author dyj
 * @since 2024-12-20
 */
@RestController
@RequestMapping("/broadcast/device_category")
public class DeviceCategoryController extends BaseController
{

    @Autowired
    private DeviceCategoryService deviceCategoryService;

    /**
     * 查询设备类型（列表）
     */
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam(required = false) String searchKey)
    {
        List<DeviceCategory> list = deviceCategoryService.list(searchKey);
        return getDataTable(list);
    }

    /**
     * 导出设备类型（列表）
     */
    @Log(title = "设备类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceCategory DeviceCategory)
    {
        List<DeviceCategory> list = deviceCategoryService.list();
        ExcelUtil<DeviceCategory> util = new ExcelUtil<DeviceCategory>(DeviceCategory.class);
        util.exportExcel(response, list, "【设备类型】数据");
    }

    /**
     * 查询设备类型（详细信息）
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(deviceCategoryService.getById(id));
    }

    /**
     * 新增设备类型
     */
    @Log(title = "【设备类型】", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
    public AjaxResult add(@RequestBody DeviceCategory DeviceCategory)
    {
        DeviceCategory.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        DeviceCategory.setUpdateTime(new Date());
        DeviceCategory.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        DeviceCategory.setCreateTime(new Date());
        deviceCategoryService.save(DeviceCategory);
        return success(DeviceCategory);
    }

    /**
     * 更新设备类型
     */
    @Log(title = "【设备类型】", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}")
    public AjaxResult update(@PathVariable("id") Long id, @RequestBody DeviceCategory DeviceCategory)
    {
        DeviceCategory.setId(id);
        DeviceCategory.setUpdateBy(SecurityUtils.getLoginUser().getUser().getNickName());
        DeviceCategory.setUpdateTime(new Date());
        deviceCategoryService.updateById(DeviceCategory);
        return success(DeviceCategory);
    }

    /**
     * 删除设备类型
     */
    @Log(title = "【设备类型】", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(deviceCategoryService.removeById(id));
    }
}
